﻿using System;
using System.IO;
using System.Linq;
using OpenSoftStore.Orm.Model;

namespace OpenSoftStore.Orm
{
    public class SpaEntities : EfToAccess
    {
        public SpaEntities()
            : base("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" +
                AppDomain.CurrentDomain.BaseDirectory + "Spa.accdb")
        {
            //UpdateDatabase();
        }
        #region IEnumerable 属性

        public void UpdateDatabase()
        {
            AddProvider();
            UpdateProvider();
            UpdateMemberTreeCode();
            UpdateStoreSet();

            if (!File.Exists(@"UpdateDatabase.config")) return;
            var streamReader = new StreamReader(@"UpdateDatabase.config", true);
            var line = streamReader.ReadLine();
            while (line != null)
            {
                if (!line.Contains("//") || !line.Contains("--") || string.IsNullOrEmpty(line)) RunSqLcmd(line);
                line = streamReader.ReadLine();
            }
            streamReader.Close();
        }

        /// <summary>
        /// 添加基础数据无供应商
        /// </summary>
        private void AddProvider()
        {
            var providers = this.Where<T_Provider>(p => p.Id == "46872df4-47b1-4875-bdbb-1f8f8d1023ee").ToList();
            if (!providers.Any())
            {
                T_Provider provider = new T_Provider();
                provider.Id = "46872df4-47b1-4875-bdbb-1f8f8d1023ee";
                provider.TreeId = "16b1c41d-f481-46c9-afb1-60837b53a2c9";
                provider.Name = "无供应商";
                provider.Remark = "此数据不要删除";
                provider.ShopId = "b0def1a7-bf25-45f2-b65a-6dc586dbfc2c";
                this.Add(provider);
            }
        }

        /// <summary>
        /// 修改正元堂供应商的TreeId
        /// </summary>
        private void UpdateProvider()
        {
            var providers = this.Where<T_Provider>(p => p.Id == "3f49bf9c-f262-4840-9dd7-a44a07064ff6").ToList();
            if (providers.Any())
            {
                var provider = providers.FirstOrDefault();
                provider.TreeId = "57af1b60-083f-4dd6-8872-db3f75b0ce68";
                this.Update(provider);
            }
        }


        /// <summary>
        /// 修改客户类别树节点Code
        /// </summary>
        private void UpdateMemberTreeCode()
        {
            var trees = this.Where<T_Tree>(p => p.Id == "33794c54-e070-42f1-94ec-8a33733382bb").ToList();
            if (trees.Any())
            {
                var tree = trees.FirstOrDefault();
                tree.Code = "1.0.";
                this.Update(tree);
            }
        }

        private void UpdateStoreSet()
        {
            var data = this.Where<T_Combo>(m => m.BuzType == "Pos" && m.ShopId == "b0def1a7-bf25-45f2-b65a-6dc586dbfc2c").ToList();
            if (!data.Where(c => c.Value2 == "是").Any())
            {
                var combo = data.FirstOrDefault();
                combo.Value2 = "是";
                this.Update(combo);
            }
        }

        #endregion
    }
}